﻿using System;
using System.Collections.Generic;
using TowerDefence_Zombie.Data;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using UnityEngine.EventSystems;
using Sirenix.OdinInspector;

namespace TowerDefence_Zombie
{
    public class ItemTowerGrid : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
    {
        [SerializeField] private int gridId;
        [SerializeField] private GameObject lockObj;

        [SerializeField, BoxGroup("炮塔相关UI")] private GameObject emptyObj;
        [SerializeField, BoxGroup("炮塔相关UI")] private CanvasGroup canvasGroup;
        [SerializeField, BoxGroup("炮塔相关UI")] private Image towerIcon;
        [SerializeField, BoxGroup("炮塔相关UI")] private TMP_Text towerLvText;

        [SerializeField, Title("合成特效"), BoxGroup("炮塔相关UI")] private UIAnimation synthesisEffect;
        [SerializeField, Title("解锁特效"), BoxGroup("炮塔相关UI")] private UIAnimation unlockEffect;

        private int _towerId;
        private int _towerLevel;
        private bool _unlockState;
        private event Action<int, ItemTowerGrid> _onBeginDrag;

        public void Init(Action<int, ItemTowerGrid> onBeginDrag)
        {
            if (GameEntry.Data.GetData<DataSynthesis>()
                .GetSynthesisGridData(gridId, out var gridData))
            {
                RefreshUnLockState(gridData.UnLockState);
                RefreshTowerInfo(gridData.TowerId, gridData.TowerLevel, false);
                _onBeginDrag += onBeginDrag;
            }
        }

        public void RefreshTowerInfo(int towerId, int level, bool isUpgraded)
        {
            _towerId = towerId;
            _towerLevel = level;

            if (towerId > 0)
            {
                var drDataTower = GameEntry.Data.GetData<DataTower>();
                var itemTowerData = drDataTower.GetTowerData(towerId);
                towerIcon.sprite = Resources.Load<Sprite>(itemTowerData.Icon);
                towerLvText.text = $"{_towerLevel + 1}";
                SetActivityState(true);
            }
            else SetActivityState(false);

            if (isUpgraded)
                synthesisEffect.Play();
        }

        public void RefreshUnLockState(bool unlockState, bool isPlayEffect = false)
        {
            _unlockState = unlockState;
            if (_unlockState)
            {
                lockObj.SetActive(false);
                if (isPlayEffect)
                    unlockEffect.Play();
            }
            else
            {
                lockObj.SetActive(true);
            }
        }

        public int GetGridRealId() => gridId;

        public void GetGridTowerInfo(out int towerId, out int towerLevel)
        {
            towerId = _towerId;
            towerLevel = _towerLevel;
        }

        public void SetActivityState(bool state)
        {
            if (state)
            {
                emptyObj.SetActive(false);
                canvasGroup.alpha = 1;
            }
            else
            {
                emptyObj.SetActive(true);
                canvasGroup.alpha = 0;
            }
        }

        public void OnBeginDrag(PointerEventData eventData)
        {
            if (_towerId <= 0 || _unlockState == false) return;

            _onBeginDrag?.Invoke(GetGridRealId(), this);
        }

        public void OnDrag(PointerEventData eventData) { }

        public void OnEndDrag(PointerEventData eventData) { }
    }
}
